/**
 * Created by aaronjin on 16/11/24.
 */

const webpack = require('webpack');
const autoprefixer = require('autoprefixer-core');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const resolve = require('path').resolve;
const css = require('./css.js');

module.exports = function (env) {
    try {

        var plugins = [];
        plugins.push(new webpack.HotModuleReplacementPlugin());
        plugins.push(new webpack.NoErrorsPlugin());
        plugins.push(css.extractSASS);
        plugins.push(css.extractLESS);

        plugins.push(new webpack.optimize.CommonsChunkPlugin({
            name: env.project,
            filename: '[name]/vendor.js',
            minChunks: function (module, count) {
                var resourceName = module.resource;
                var reg = /node_modules/;
                if (reg.test(resourceName)) {
                    return true;
                }
                return false;
            }
        }));
        plugins.push(new HtmlWebpackPlugin({
            template: './src/index.html',
            //favicon: './src/favicon.ico',
            filename: 'index.html',
        }));

        //plugins.push(
        //    new ExtractTextPlugin('css/[name].bundle.css', {})
        //);


        if (env.ver != 'dev' && env.project=='react') {

            //plugins.push(new webpack.DefinePlugin({
            //    'process.env.NODE_ENV': '"development"'
            //}));
            plugins.push(new webpack.optimize.UglifyJsPlugin({
                compress: {
                    warnings: false,
                    drop_debugger: true,
                    drop_console: true
                }
            }));
        }
        return plugins;
    } catch (e) {
        throw new Error(e);
    }

};

